//
//  HEFreeConsulViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/20.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEFreeConsulViewController.h"
#import "HEDiagnosisCardCell.h"
#import "HEAIUserTextCell.h"
#import "HEAIMsgTextCell.h"
#import "HEAIMissonCell.h"
#import "HEAISingleQuestionCell.h"
#import "HEAIEndReportCell.h"
#import "HEAIEndNoticeCell.h"
#import "HEAIMultiQuestionCell.h"

#import "HEAIMsgModel.h" 
#import "HEAISubItemModel.h"

#import "HEAIConsultModel.h"

#import "HEAIChatInputView.h"
#import "HEAIDiesaeMsgModel.h"
#import "HESpeechManager.h"
#import "HEFindDoctorViewController.h"
#import "HEConsulItemViewController.h"
#import "HEDiesaseDetailsViewController.h"
#import "HEConsulReportViewController.h"
#import "HEConsulRecoderListViewController.h"
#import "HESoundHUD.h"
#import "HESpeechListener.h"

@interface HEFreeConsulViewController ()<QMUITextViewDelegate, UITableViewDelegate, UITableViewDataSource, HEDiagnosisCardCellDelegate, HEAISingleQuestionCellDelegate, HEAIMultiQuestionCellDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray <GDFiterCellModel *>*sections;
@property (nonatomic, strong) NSMutableArray <GDFormModel *>*datasource;

@property(nonatomic, strong) QMUITextView *textView;
@property(nonatomic, strong) UIView *tabbarTool;
@property(nonatomic, strong) UIView *tabbarWarp;
@property(nonatomic, strong) UIView *voiceWarp;
@property(nonatomic, strong) UIButton *yuyinView;
@property(nonatomic, strong) UIView *rightView;

@property(nonatomic, assign) BOOL fristInto;
@property(nonatomic, assign) CGFloat transformY;

@property(nonatomic, assign) CGFloat panelheight;

@property (nonatomic, strong) NSMutableArray <HEAIConsultModel *> *personConsArr; // 疾病自诊


@property(nonatomic, strong) HEAIChatInputView *tabbarView;

@property(nonatomic, assign) BOOL isAnswering;

@property(nonatomic, assign) BOOL hideTabbarTool;
@property(nonatomic, strong) HEAskQuestionModel *currentQuestion;

@end

@implementation HEFreeConsulViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotify:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    if (!self.tableView) {
        [self initUI];
        [self initCell];
        [self initTabbarTool];
        [self loadData];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[HESpeechManager sharedManager] stopSpeech];
}

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}
 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"免费咨询";

    _fristInto = YES;
    _panelheight = 120;
    
//    [self initUI];
//    [self initCell];
//
//    [self initTabbarTool];
//
////    _tabbarView = [HEAIChatInputView new];
////    _tabbarView.tableView = self.tableView;
////    [self.view addSubview:_tabbarView];
////
////    [_tabbarView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.left.right.equalTo(self.view);
////        make.bottom.equalTo(self.view);
////        make.height.equalTo(@49);
////    }];
//
//
//
//    [self loadData];
}

 
- (void)initUI {
    
    // 设置导航栏按钮
    [self zx_setRightBtnWithImgName:@"unmute" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        if ([HESpeechManager sharedManager].canSpeaker) {
            [[HESpeechManager sharedManager] enable:NO];
            [self.zx_navRightBtn setImage:IMAGE_NAMED(@"mute") forState:UIControlStateNormal];
        }else {
            [[HESpeechManager sharedManager] enable:YES];
            [self.zx_navRightBtn setImage:IMAGE_NAMED(@"unmute") forState:UIControlStateNormal];
        }
    }];
    
   CGFloat tabbarHeight = 49 + KSafeTabbarHeight;
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight- ZXNavBarHeight - tabbarHeight) style:UITableViewStylePlain];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    
   _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   _tableView.scrollsToTop = YES;
   
    self.tableView.backgroundColor = CViewBgColor;
    //    self.tableView.backgroundColor = UIColor.greenColor;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)initCell {
   // 总段
   _sections = [NSMutableArray array];
    
   _datasource = @[].mutableCopy;
    
   NSMutableArray *classArr = [NSMutableArray array];
 

   // 疾病自诊
    GDFiterCellModel *diagModel = [GDFiterCellModel new];
    diagModel.cellKlass = HEDiagnosisCardCell.class;
    
    // 用户回复文本
     GDFiterCellModel *textModel = [GDFiterCellModel new];
    textModel.cellKlass = HEAIUserTextCell.class;
    
    // ai文本消息
    GDFiterCellModel *aiTextModel = [GDFiterCellModel new];
    aiTextModel.cellKlass = HEAIMsgTextCell.class;
    
    // ai猜一猜
    GDFiterCellModel *aiMissonModel = [GDFiterCellModel new];
    aiMissonModel.cellKlass = HEAIMissonCell.class;
    
    // 单选题
    GDFiterCellModel *singModel = [GDFiterCellModel new];
    singModel.cellKlass = HEAISingleQuestionCell.class;
    
    // 结束问答 生成报告
    GDFiterCellModel *reportModel = [GDFiterCellModel new];
    reportModel.cellKlass = HEAIEndReportCell.class;
    
    GDFiterCellModel *endNoticeModel = [GDFiterCellModel new];
    endNoticeModel.cellKlass = HEAIEndNoticeCell.class;
 
    // 多选题
    GDFiterCellModel *multModel = [GDFiterCellModel new];
    multModel.cellKlass = HEAIMultiQuestionCell.class;
    
 
    
   // 添加的时候注意顺序，暂未对顺序做处理
   classArr = @[diagModel, textModel, aiTextModel, aiMissonModel, singModel, reportModel, endNoticeModel, multModel].mutableCopy;
   [self.tableView mc_registerCellByFiterModels:classArr];
    
 
    [[UserManager sharedUserManager] getAIData: self];

     [self.datasource addObjectsFromArray:[UserManager sharedUserManager].aiDatasourceArr];
     [self.sections addObjectsFromArray:[UserManager sharedUserManager].aiFiterCellArr];

 
   [_tableView reloadData];
}
 
  
- (void)initTabbarTool {
    
    CGFloat tabbarHeight = 49 + KSafeTabbarHeight;

    UIView *tabbarTool = [UIView new];
    tabbarTool.qmui_borderPosition = QMUIViewBorderPositionTop;
    tabbarTool.qmui_borderWidth = 1;
    tabbarTool.qmui_borderColor = CSplitLineColor;
    _tabbarTool = tabbarTool;
    [self.view addSubview:tabbarTool];
    
    UIView *tabbarWarp = [UIView new];
    _tabbarWarp = tabbarWarp;
    [tabbarTool addSubview:tabbarWarp];
    
    
    [tabbarWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tabbarTool);
        make.height.equalTo(@49);
        make.left.right.equalTo(tabbarTool);
    }];
    
//    [tabbarTool mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.view);
//        make.bottom.equalTo(self.view);
//        make.height.equalTo(@49);
//    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(ZXNavBarHeight);
        make.left.and.width.equalTo(self.view);
        make.bottom.equalTo(tabbarTool.mas_top);
    }];
    [tabbarTool mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@(49));
        make.bottom.equalTo(self.view).offset(-ZXSafeAreaBottom);
    }];
    
    
    
    // 语音
    UIButton *yuyinView = [UIButton buttonWithType:UIButtonTypeCustom];
    [yuyinView setImage:IMAGE_NAMED(@"voice") forState:UIControlStateNormal];
    [tabbarWarp addSubview:yuyinView];
    [yuyinView addTarget:self action:@selector(voiceClick:) forControlEvents:UIControlEventTouchUpInside];
    _yuyinView = yuyinView;
 
    //
    UIView *rightView = [UIView new];
    [_tabbarTool addSubview:rightView];
    _rightView = rightView;
    
    // 输入框
    _textView = [QMUITextView  new];
    _textView.placeholder = @"开始免费咨询";
    _textView.placeholderColor = CNavSubTitleColor; // 自定义 placeholder 的颜色
    _textView.placeholderMargins = UIEdgeInsetsMake(0, 7, 0, 7);
    _textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    _textView.layer.cornerRadius = 4;
    [_textView setDelegate:self];
    [tabbarWarp addSubview:_textView];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tabbarWarp).mas_offset(6);
        make.left.equalTo(yuyinView.mas_right).offset(10);
        make.right.equalTo(_rightView.mas_left).offset(-10);
        make.bottom.equalTo(tabbarWarp).mas_offset(-6);
    }];
    
    [yuyinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tabbarWarp);
        make.left.equalTo(tabbarWarp).offset(10);
        make.width.equalTo(@25);
        make.height.equalTo(@27);
    }];
     
 
    [self nomalStatus];
}

- (void)nomalStatus {
    [_rightView removeAllSubviews];

    [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_tabbarWarp);
        make.right.equalTo(_tabbarWarp).offset(-10);
        make.width.mas_offset(32 * 2 + 10);
        make.height.mas_offset(49);
    }];
    
    // 咨询记录
    UIImageView *reportView = [UIImageView new];
    reportView.image = IMAGE_NAMED(@"service_his");
    reportView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightView addSubview:reportView];
    reportView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reportRecoderClick)];
    [reportView addGestureRecognizer:reTap];

    UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendBtn setImage:IMAGE_NAMED(@"add_media") forState:UIControlStateNormal];
    [sendBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    sendBtn.titleLabel.font = DYBlodFont(15);
    [_rightView addSubview:sendBtn];
    [sendBtn addTarget:self action:@selector(selectConsulClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSArray *arr1 = @[reportView, sendBtn];
    [arr1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView).offset(5);
        make.bottom.equalTo(_rightView).offset(-5);
    }];
    [arr1 mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
}

- (void)reportRecoderClick {
    HEConsulRecoderListViewController *vc = [HEConsulRecoderListViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)selectConsulClick {
    HEConsulItemViewController *vc = [HEConsulItemViewController new];
    vc.selectBlock = ^(HEAIConsultModel * _Nonnull model) {
        [self dealUserReply:model.ask];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)textStatus {
    [_rightView removeAllSubviews];
    
    [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_tabbarWarp);
        make.right.equalTo(_tabbarWarp).offset(-0);
        make.width.mas_offset(32 + 70 + 10);
        make.height.mas_offset(49);
    }];
    
    // 咨询记录
    UIImageView *reportView = [UIImageView new];
    reportView.image = IMAGE_NAMED(@"service_his");
    reportView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightView addSubview:reportView];
    
    // +
    UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendBtn setTitle:@"提交" forState:UIControlStateNormal];
    [sendBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    sendBtn.backgroundColor = CNavBgColor;
    sendBtn.titleLabel.font = DYBlodFont(15);
    [_rightView addSubview:sendBtn];
    
    [sendBtn addTarget:self action:@selector(sendMsg) forControlEvents:UIControlEventTouchUpInside];
     
    [reportView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView).offset(5);
        make.bottom.equalTo(_rightView).offset(-5);
        make.left.equalTo(_rightView);
        make.width.offset(30);
    }];
    
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView).offset(5);
        make.bottom.equalTo(_rightView).offset(-5);
        make.right.equalTo(_rightView.mas_right);
        make.left.equalTo(reportView.mas_right).offset(10);
    }];
    
//    NSArray *arr1 = @[reportView, sendBtn];
//    [arr1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_rightView).offset(5);
//        make.bottom.equalTo(_rightView).offset(-5);
//    }];
//    [arr1 mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
}

- (void)answerQuestionStatus {
    [_rightView removeAllSubviews];
    
    [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_tabbarWarp);
        make.right.equalTo(_tabbarWarp).offset(-0);
        make.width.mas_offset(60 + 60 + 20);
        make.height.mas_offset(49);
    }];
    
    // 咨询记录
    UIImageView *reportView = [UIImageView new];
    reportView.image = IMAGE_NAMED(@"service_his");
    reportView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightView addSubview:reportView];
    

    QMUIFillButton *endBtn = [[QMUIFillButton alloc] initWithFillColor:CPriceColor titleTextColor:KWhiteColor];
    endBtn.titleLabel.font = UIFontMake(16);
    [endBtn setTitle:@"结束" forState:UIControlStateNormal];
    [_rightView addSubview:endBtn];
    [endBtn addTarget:self action:@selector(endBtnClick) forControlEvents:UIControlEventTouchUpInside];

    
    // +
    QMUIFillButton *sendBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:KWhiteColor];
    [sendBtn setTitle:@"提交" forState:UIControlStateNormal];
    [sendBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    sendBtn.backgroundColor = CNavBgColor;
    sendBtn.titleLabel.font = DYBlodFont(15);
    [_rightView addSubview:sendBtn];
    
    [sendBtn addTarget:self action:@selector(sendMsg) forControlEvents:UIControlEventTouchUpInside];
     
    [endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView).offset(8);
        make.bottom.equalTo(_rightView).offset(-8);
        make.left.equalTo(_rightView);
        make.width.offset(60);
    }];
    
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView).offset(8);
        make.bottom.equalTo(_rightView).offset(-8);
        make.right.equalTo(_rightView).offset(-10);
        make.width.offset(60);
    }];
    
//    NSArray *arr1 = @[reportView, sendBtn];
//    [arr1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_rightView).offset(5);
//        make.bottom.equalTo(_rightView).offset(-5);
//    }];
//    [arr1 mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
}

- (void)endBtnClick {
    [self endQuestion];
}

- (void)sendMsg {
    
    [self dealUserReply: _textView.text];

    if (_isAnswering) {
        [self userAnswerRequest: _textView.text];
    }
    _textView.text = nil;
}


- (void)voiceClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    CGFloat h = self.tabbarTool.top - ZXNavBarHeight;

    [_tabbarTool addSubview: self.voiceWarp];
    
    if (sender.selected) {
        [self.view setNeedsUpdateConstraints];
        [self.view updateConstraintsIfNeeded];
        
        [self.voiceWarp mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tabbarWarp.mas_bottom);
            make.left.right.equalTo(_tabbarTool);
            make.width.mas_offset(KScreenWidth);
            make.height.mas_offset(_panelheight);
        }];
        // 立即绘制
        [self.voiceWarp.superview layoutIfNeeded];
        
        [UIView animateWithDuration:.3 animations:^{
            self.voiceWarp.alpha = 1;
            self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, h - _panelheight );
            [self.tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.tabbarWarp.height + _panelheight);
            }];
            [self.tabbarTool.superview layoutIfNeeded];
        }];
    }else {
        [self hideViocePanel];
    }
}

- (UIView *)voiceWarp {
    if (!_voiceWarp) {
        _voiceWarp = [UIView new];
        
        QMUIButton *check = [[QMUIButton alloc] init];
        check.imagePosition = QMUIButtonImagePositionTop;// 将图片位置改为在文字下方
        check.tintColorAdjustsTitleAndImage = CNavSubTitleColor;
        check.adjustsImageTintColorAutomatically = NO;
        check.spacingBetweenImageAndTitle = 10;
        [check setImage:UIImageMake(@"input_record") forState:UIControlStateNormal];
        [check setTitle:@"按住我震动后语音输入" forState:UIControlStateNormal];
        check.titleLabel.font = DYFont(15);
        [_voiceWarp addSubview:check];
        [check addTarget:self action:@selector(startSpeakClick) forControlEvents:UIControlEventTouchDown];
        [check addTarget:self action:@selector(stopSpeakClick) forControlEvents:UIControlEventTouchUpInside];
        [check addTarget:self action:@selector(stopSpeakClick) forControlEvents:UIControlEventTouchUpOutside];

        [check mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_voiceWarp);
            make.centerY.equalTo(_voiceWarp);
            make.centerX.equalTo(_voiceWarp);
            make.bottom.equalTo(_voiceWarp);
        }];
    }
    return _voiceWarp;
}

- (void)startSpeakClick {
 
    [[HESoundHUD sharedManager] show];
    
    HESpeechListener *sp = [HESpeechListener sharedManager];
    [sp startRecognizer];
    sp.textBlock = ^(NSString * _Nonnull text, BOOL status) {
        if (status) {
            self.textView.text = text;
        }else {
            [[HESoundHUD sharedManager] hide];
        }
    };
}

- (void)stopSpeakClick {
    [[HESpeechListener sharedManager] endRecognizer];
    [[HESoundHUD sharedManager] hide];
}

#pragma mark -
- (void)loadData {
    
    self.personConsArr = @[].mutableCopy;
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KServiceTagAsk parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.personConsArr addObjectsFromArray:[HEAIConsultModel mj_objectArrayWithKeyValuesArray:response.data]];

        [self dealConsulData];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)userReplyTextRequest:(NSString *)text {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KAIAnswer parameters:@{@"text":text} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        HEAIDiesaeMsgModel *aiMsg = [HEAIDiesaeMsgModel mj_objectWithKeyValues: response.data];
        [self.personConsArr addObjectsFromArray:[HEAIConsultModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self dealAIReply:aiMsg];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)userAnswerRequest:(NSString *)text {
    
    if ([DYTools isBlankString:text]) {
        [Dialog toastBottom:@"请输入您的答案"];
        return;
    }
    
    HEAskQuestionModel *params = [HEAskQuestionModel new];
    params.symptom_id = _currentQuestion.symptom_id;
    params.ask_reponse = text;
    params.inst_id = _currentQuestion.inst_id;
    params.text = _currentQuestion.text;
    params.ask_field = _currentQuestion.ask_field;
    params.inq_points = _currentQuestion.inq_points;
    params.seq = _currentQuestion.seq;
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KAIAnswer parameters: params success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        HEAIDiesaeMsgModel *aiMsg = [HEAIDiesaeMsgModel mj_objectWithKeyValues: response.data];
        [self.personConsArr addObjectsFromArray:[HEAIConsultModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self dealAIReply:aiMsg];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)endQuestion {
    HEAskQuestionModel *params = [HEAskQuestionModel new];
    params.symptom_id = _currentQuestion.symptom_id;
    params.inst_id = _currentQuestion.inst_id;
    params.text = _currentQuestion.text;
    params.inq_points = _currentQuestion.inq_points;
    params.seq = @"999";
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KAIAnswer parameters: params success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        HEAIDiesaeMsgModel *aiMsg = [HEAIDiesaeMsgModel mj_objectWithKeyValues: response.data];
        [self.personConsArr addObjectsFromArray:[HEAIConsultModel mj_objectArrayWithKeyValuesArray:response.data]];
        
        if (response.success) {
            [self endQuestionState];
            [self dealEndQuestion];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)endQuestionState {
    for (GDFormModel *model in self.datasource) {
        model.zx_model.isFinish = YES;
    }
    [_tableView reloadData];
}

#pragma mark -

- (void)dealUserReply: (NSString *)text {
    
    if ([DYTools isBlankString:text]) {
        [Dialog toastBottom:@"请输入您的答案"];
        return;
    }
    
    HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
    lastModel.headImage = [UserManager sharedUserManager].curUserInfo.avatar;
    lastModel.uid = [DYTools createUuid];
    lastModel.type = HEAIMsgTypeUserText;
    lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
    lastModel.content = text;
  
    GDFiterCellModel *fiterModel = [GDFiterCellModel new];
    fiterModel.cellKlass = [lastModel getCellClass];
    [_sections addObject: fiterModel];

    GDFormModel *fmodel = [GDFormModel new];
    fmodel.zx_model = lastModel;
    fmodel.proxyVC = self;
    
    [_datasource addObject:fmodel];
    
    [[UserManager sharedUserManager] saveAIDataWithFiter:_sections forms:_datasource];
    
 
    // 刷新tableView
    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    if (!_isAnswering) {
        [self userReplyTextRequest: lastModel.content];
    }
}

- (void)dealConsulData {
 
    HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
    lastModel.headImage = @"head";
    lastModel.uid = [DYTools createUuid];
    lastModel.type = HEAIMsgTypeDiesaeConsult;
    lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
 
    for (HEAIConsultModel *item in _personConsArr) {
        HEAISubItemModel *model = [[HEAISubItemModel alloc] init];
        model.content = item.ask;
        [lastModel.subDataArr addObject:model];
    }
    
    GDFiterCellModel *diagModel = [GDFiterCellModel new];
    diagModel.cellKlass = [lastModel getCellClass];

    GDFormModel *fmodel = [GDFormModel new];
    fmodel.zx_model = lastModel;
    fmodel.proxyVC = self;
    

    HEAIMsgModel *endModel = _datasource.lastObject.zx_model;
    
    if (endModel.type == HEAIMsgTypeDiesaeConsult) {
        endModel.uid = lastModel.uid;
        endModel.createDate = lastModel.createDate;
    }else {
        [_sections addObject: diagModel];
        [_datasource addObject:fmodel];
    }
    
    
    [_tableView reloadData];
    [[UserManager sharedUserManager] saveAIDataWithFiter:_sections forms:_datasource];

    [self scrollToBottom];
}

- (void)dealAIReply:(HEAIDiesaeMsgModel *)model {

    if ([@"5" isEqualToString: model.type]) { // 疾病相关列表（如：输入心脏病）
        
        if (model.diseaseList.count > 0) {
            
            HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
            lastModel.headImage = [UserManager sharedUserManager].curUserInfo.avatar;
            lastModel.uid = [DYTools createUuid];
            lastModel.type = HEAIMsgTypeAIMisson;
            lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
            for (HEDiseaseInfoModel *item in model.diseaseList) {
                HEAISubItemModel *model = [[HEAISubItemModel alloc] init];
                model.content = item.disease_name;
                model.attachModel = item;
                [lastModel.subDataArr addObject:model];
            }
            
            GDFiterCellModel *fiterModel = [GDFiterCellModel new];
            fiterModel.cellKlass = [lastModel getCellClass];
            [_sections addObject: fiterModel];

            GDFormModel *fmodel = [GDFormModel new];
            fmodel.zx_model = lastModel;
            fmodel.proxyVC = self;
            [_datasource addObject:fmodel];

        }else {
            HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
            lastModel.headImage = [UserManager sharedUserManager].curUserInfo.avatar;
            lastModel.uid = [DYTools createUuid];
            lastModel.type = HEAIMsgTypeAIText;
            lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
            lastModel.content = @"没有找到，换个词试试~";
          
            GDFiterCellModel *fiterModel = [GDFiterCellModel new];
            fiterModel.cellKlass = [lastModel getCellClass];
            [_sections addObject: fiterModel];

            GDFormModel *fmodel = [GDFormModel new];
            fmodel.zx_model = lastModel;
            fmodel.proxyVC = self;
            
            [_datasource addObject:fmodel];
        }
    }else if([@"1" isEqualToString: model.type]) { // 系统对话结果，只显示
        
    }else if([@"2" isEqualToString: model.type]) { // 自诊开始答题（quest_type ： 0用户录入答案、1单选，2多选）
        
        _isAnswering = YES;
        _currentQuestion = model.askQuestion;
        
        [[HESpeechManager sharedManager] startWithText: model.askQuestion.ask];

        if ([@"0" isEqualToString: model.askQuestion.quest_type]) {
            HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
            lastModel.headImage = [UserManager sharedUserManager].curUserInfo.avatar;
            lastModel.uid = [DYTools createUuid];
            lastModel.type = HEAIMsgTypeAIText;
            lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
            lastModel.content = model.askQuestion.ask;
          
            GDFiterCellModel *fiterModel = [GDFiterCellModel new];
            fiterModel.cellKlass = [lastModel getCellClass];
            [_sections addObject: fiterModel];

            GDFormModel *fmodel = [GDFormModel new];
            fmodel.zx_model = lastModel;
            fmodel.proxyVC = self;
            
            [_datasource addObject:fmodel];
            
            [self showTarBarTool];
            [self answerQuestionStatus];
        }else if ([@"1" isEqualToString:model.askQuestion.quest_type]) {
            HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
            lastModel.headImage = @"head";
            lastModel.uid = [DYTools createUuid];
            lastModel.type = HEAIMsgTypeAISingleQuestion;
            lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
            lastModel.content = model.askQuestion.ask;

            for (HEQuestionItemModel *item in model.askQuestion.dicts) {
                HEAISubItemModel *model = [[HEAISubItemModel alloc] init];
                model.content = item.ask_range_value;
                [lastModel.subDataArr addObject:model];
            }

            GDFiterCellModel *fiterModel = [GDFiterCellModel new];
            fiterModel.cellKlass = [lastModel getCellClass];
            [_sections addObject: fiterModel];

            GDFormModel *fmodel = [GDFormModel new];
            fmodel.zx_model = lastModel;
            fmodel.proxyVC = self;
            [_datasource addObject:fmodel];
            
            [self hideTarBarToolWhenAnswer];

        }else if ([@"2" isEqualToString:model.askQuestion.quest_type]) {
            HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
            lastModel.headImage = @"head";
            lastModel.uid = [DYTools createUuid];
            lastModel.type = HEAIMsgTypeAIMultiQuestion;
            lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
            lastModel.content = model.askQuestion.ask;

            for (HEQuestionItemModel *item in model.askQuestion.dicts) {
                HEAISubItemModel *model = [[HEAISubItemModel alloc] init];
                model.content = item.ask_range_value;
                [lastModel.subDataArr addObject:model];
            }

            GDFiterCellModel *fiterModel = [GDFiterCellModel new];
            fiterModel.cellKlass = [lastModel getCellClass];
            [_sections addObject: fiterModel];

            GDFormModel *fmodel = [GDFormModel new];
            fmodel.zx_model = lastModel;
            fmodel.proxyVC = self;
            [_datasource addObject:fmodel];
            
            [self hideTarBarToolWhenAnswer];
        }
    }else if([@"3" isEqualToString: model.type]) { // 问答结束
        [self endQuestionState];
        [self dealEndQuestion];
    }
    
    [[UserManager sharedUserManager] saveAIDataWithFiter:_sections forms:_datasource];
    
    // 刷新tableView
    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (void)dealEndQuestion {
    
    _isAnswering = NO;
    
    [self showTarBarTool];
    [self nomalStatus];

    HEAIMsgModel *lastModel = [[HEAIMsgModel alloc] init];
    lastModel.headImage = @"head";
    lastModel.uid = [DYTools createUuid];
    lastModel.type = HEAIMsgTypeAIReport;
    lastModel.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
    lastModel.content = @"问答完成，小X已为您生成Al结构化病例，您可以根据需求选择合适您的医生问诊";
    lastModel.inst_id = _currentQuestion.inst_id;
  
    GDFiterCellModel *fiterModel = [GDFiterCellModel new];
    fiterModel.cellKlass = [lastModel getCellClass];
    [_sections addObject: fiterModel];

    GDFormModel *fmodel = [GDFormModel new];
    fmodel.zx_model = lastModel;
    fmodel.proxyVC = self;
    
    [_datasource addObject:fmodel];
    
    
    [[HESpeechManager sharedManager] startWithText: lastModel.content];

    
    HEAIMsgModel *lastModel2 = [[HEAIMsgModel alloc] init];
    lastModel2.headImage = @"head";
    lastModel2.uid = [DYTools createUuid];
    lastModel2.type = HEAIMsgTypeAIFinish;
    lastModel2.createDate = [NSDate getNowDay: NSDateFormaterModeTimeMinute];
  
    GDFiterCellModel *fiterModel2 = [GDFiterCellModel new];
    fiterModel2.cellKlass = [lastModel2 getCellClass];
    [_sections addObject: fiterModel2];

    GDFormModel *fmodel2 = [GDFormModel new];
    fmodel2.zx_model = lastModel2;
    fmodel2.proxyVC = self;
    
    [_datasource addObject: fmodel2];
    
    [[UserManager sharedUserManager] saveAIDataWithFiter:_sections forms:_datasource];

 
    // 刷新tableView
    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
     
    //    _currentQuestion = nil;
}

- (void)hideTarBarToolWhenAnswer {
    self.hideTabbarTool = YES;
    [self hideViocePanel];
    [_textView resignFirstResponder];
    
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
 
    [UIView animateWithDuration:.3 animations:^{
 
        self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, kScreenHeight - ZXNavBarHeight );

        [self.tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [self.tabbarTool layoutIfNeeded];
        [self.tabbarTool.superview layoutIfNeeded];
    }];
}

- (void)showTarBarTool {
    self.hideTabbarTool = NO;

    [self hideViocePanel];
    [_textView resignFirstResponder];
    
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
  
    [self updateChatBarKeyBoardConstraints:0];
//    [UIView animateWithDuration:.3 animations:^{
//
//        self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, kScreenHeight - ZXNavBarHeight - 49 );
//
//        [self.tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(49);
//            make.bottom.offset(-ZXSafeAreaBottom);
//        }];
//        [self.tabbarTool.superview layoutIfNeeded];
//    }];
}


#pragma mark -
    
/**
 *  点击了return按钮(键盘最右下角的按钮)就会调用
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
 
/**
 *  当键盘改变了frame(位置和尺寸)的时候调用
 */
- (void)keyboardWillChangeFrameNotify:(NSNotification*)notify {
    CGFloat tabbarHeight = self.tabbarTool.height + KSafeTabbarHeight;
    _fristInto = NO;

    
    // 0.取出键盘动画的时间
    CGFloat duration = [notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 1.取得键盘最后的frame
    CGRect keyboardFrame = [notify.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat keyBordOriginY = keyboardFrame.origin.y;
    BOOL iOS7 = ([[[UIDevice currentDevice] systemVersion] doubleValue] < 8.0);
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (iOS7 && (orientation == UIDeviceOrientationLandscapeLeft
                 || orientation == UIDeviceOrientationLandscapeRight)) {
        keyBordOriginY = keyboardFrame.origin.x + keyboardFrame.size.width;
    }
 
    // 2.计算控制器的view需要平移的距离
    CGFloat transformY = keyBordOriginY - self.view.frame.size.height;
    _transformY = transformY;
    
    // 3.执行动画
//    [UIView animateWithDuration:duration animations:^{
//        self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - tabbarHeight + transformY );
//
//        self.tabbarTool.transform = CGAffineTransformMakeTranslation(0, transformY);
////        [self scrollTableViewToBottom];
//
//        [self scrollToBottom];
//    }];
    
    [self updateChatBarKeyBoardConstraints: transformY];
//    [self scrollToBottomAnimated: YES];
}

- (void)updateChatBarKeyBoardConstraints:(CGFloat)keyboardHeight {
    
    // 告诉self.view约束需要更新
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    
    KWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat bottomMargin = keyboardHeight == 0 ? -ZXSafeAreaBottom : 0;
        bottomMargin = weakSelf.hideTabbarTool ? 0 : bottomMargin; // d
        [self.tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(keyboardHeight + bottomMargin);
        }];
        [self.tabbarTool.superview layoutIfNeeded];
    } completion:nil];
}

- (void)scrollToBottomAnimated:(BOOL)animated {
//    if (!self.allowScrollToBottom) {
//        return;
//    }
    NSInteger rows = [self.tableView numberOfRowsInSection:0];
    if (rows > 0) {
        dispatch_block_t scrollBottomBlock = ^ {
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
                                  atScrollPosition:UITableViewScrollPositionBottom
                                          animated:animated];
        };
        if (animated) {
            //when use `estimatedRowHeight` and `scrollToRowAtIndexPath` at the same time, there are some issue.
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                scrollBottomBlock();
            });
        } else {
            scrollBottomBlock();
        }
        
    }
}

- (void)scrollToBottom
{
    [self.tableView layoutIfNeeded];
    CGFloat yOffset = 0; //设置要滚动的位置 0最顶部 CGFLOAT_MAX最底部
    if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
        yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
    }
    [self.tableView setContentOffset:CGPointMake(0, yOffset) animated: !self.fristInto];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self scrollToBottom];
}

#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
 
    CGFloat he = height + 6 *2;
    CGFloat offest = he - _tabbarWarp.height;
 
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
    if (he > _tabbarTool.height) {
        
        [_tabbarWarp mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(he);
        }];
        
        [_tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(_tabbarTool.height + offest);
        }];
    }else {
        if (he > 49) {
            [_tabbarWarp mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(he);
            }];
            
            [_tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(_tabbarTool.height + offest);
            }];
        }else {
            [_tabbarWarp mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(49);
            }];
            
            [_tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset( _yuyinView.isSelected ? (_panelheight + 49) : 49);
            }];
        }
    }
    [self.tabbarTool.superview layoutIfNeeded];

    self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, _tabbarTool.top - ZXNavBarHeight);
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}
 
- (void)textViewDidChange:(UITextView *)textView {
 
    if (!_isAnswering) {
        if (textView.text.length > 0) {
            [self textStatus];
        }else {
            [self nomalStatus];
        }
    }
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_textView resignFirstResponder];
}

#pragma mark -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     
}

- (void)hideViocePanel {
    
    _yuyinView.selected = NO;
    CGFloat h = self.tabbarTool.top - ZXNavBarHeight;

    // 告诉self.view约束需要更新
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];

    [UIView animateWithDuration:.3 animations:^{
        self.voiceWarp.alpha = 0;
        
        self.tableView.frame = CGRectMake(0, ZXNavBarHeight, KScreenWidth, h + _panelheight);

        [self.tabbarTool mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.tabbarWarp.height);
        }];
        //告知父类控件绘制，不添加注释的这两行的代码无法生效
        [self.tabbarTool.superview layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.voiceWarp removeFromSuperview];
    }];
}

#pragma mark - UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return _datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   GDFiterCellModel *fiterModel = _sections[indexPath.row];
    GDFormModel *fmodel = _datasource[indexPath.row];
   
   UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, fmodel, indexPath);
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
    GDFormModel *fmodel = _datasource[indexPath.row];

//    if (fiterModel.cellType == GDFiterCellTypeSelect_TEXTView) {
//        if (fiterModel.cellHeight > 0) {
//            return fiterModel.cellHeight;
//        }else {
//            return 60;
//        }
//    }
   return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
              [cell refreshContent:fiterModel formModel: fmodel indexPath:indexPath];
   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFormModel *fmodel = _datasource[indexPath.row];
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
 
    if ([fiterModel.cellKlass isEqual: HEAIEndNoticeCell.class]) {
        HEFindDoctorViewController *vc = [HEFindDoctorViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([fiterModel.cellKlass isEqual: HEAIEndReportCell.class]) {
        HEConsulReportViewController *vc = [HEConsulReportViewController new];
        vc.inst_id = fmodel.zx_model.inst_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
 
#pragma mark - HYBTestCellDelegate
- (void)reloadCellHeightForModel:(HEAIMsgModel *)model atIndexPath:(NSIndexPath *)indexPath {
  [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - HEDiagnosisCardCellDelegate
- (void)reloadCellDataForModel:(GDFormModel *)model {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KServiceTagAsk parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        NSArray * arr = [HEAIConsultModel mj_objectArrayWithKeyValuesArray:response.data];
        if (response.success) {
            
            [model.zx_model.subDataArr removeAllObjects];
            for (HEAIConsultModel *item in arr) {
                HEAISubItemModel *subItem = [[HEAISubItemModel alloc] init];
                subItem.content = item.ask;
                [model.zx_model.subDataArr addObject: subItem];
            }
        }
        
        dispatch_main_async_safe(^{
            [model.zx_model refreshData];
            [self.tableView reloadData];
        })
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)selectDiesaseWithModel:(HEAISubItemModel *)model {
    
    [self dealUserReply: model.content];
}

- (void)startAnswerQuestion {
    
}


#pragma mark - HEAISingleQuestionCellDelegate
- (void)answerQuestionSingleWith:(HEAIMsgModel *)model {

    [self userAnswerRequest:model.userAnswerContent];
}

- (void)endQuestionSingleWith:(HEAIMsgModel *)model {
    [self endQuestion];
}

@end
